home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / RCOPS.C < prev    next >
Text File  |  1991-09-23  |  10KB  |  458 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)rcops.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* library headers */
  21. #include <blkio.h>
  22.  
  23. /* local headers */
  24. #include "lseq_.h"
  25.  
  26. /*man---------------------------------------------------------------------------
  27. NAME
  28.      ls_rcalloc - allocate memory for lseq record
  29.  
  30. SYNOPSIS
  31.      #include "lseq_.h"
  32.  
  33.      lsrec_t *ls_rcalloc(lsp)
  34.      lseq_t *lsp;
  35.  
  36. DESCRIPTION
  37.      The ls_rcalloc function creates a record of the appropriate
  38.      configuration for lseq lsp and initializes it.  The address of
  39.      the record created is returned.
  40.  
  41.      ls_rcalloc will fail if one or more of the following is true:
  42.  
  43.      [EINVAL]       lsp is not a valid lseq pointer.
  44.      [ENOMEM]       Not enough memory is available for
  45.                     allocation by the calling process.
  46.      [LSENOPEN]     lsp is not open.
  47.  
  48. SEE ALSO
  49.      ls_rcfree, ls_rcinit.
  50.  
  51. DIAGNOSTICS
  52.      On failure, a value of NULL is returned, and errno set to
  53.      indicate the error.
  54.  
  55. ------------------------------------------------------------------------------*/
  56. #ifdef AC_PROTO
  57. lsrec_t *ls_rcalloc(lseq_t *lsp)
  58. #else
  59. lsrec_t *ls_rcalloc(lsp)
  60. lseq_t *lsp;
  61. #endif
  62. {
  63.     lsrec_t *lsrp = NULL;
  64. #ifdef DEBUG
  65.     /* validate arguments */
  66.     if (!ls_valid(lsp)) {
  67.         LSEPRINT;
  68.         errno = EINVAL;
  69.         return NULL;
  70.     }
  71.  
  72.     /* check if not open */
  73.     if (!(lsp->flags & LSOPEN)) {
  74.         LSEPRINT;
  75.         errno = LSENOPEN;
  76.         return NULL;
  77.     }
  78. #endif
  79.     /* allocate storage for main record structure */
  80.     /* (calloc is used throughout to automatically set all bits 0) */
  81.     lsrp = (lsrec_t *)calloc((size_t)1, sizeof(lsrec_t));
  82.     if (lsrp == NULL) {
  83.         LSEPRINT;
  84.         errno = ENOMEM;
  85.         return NULL;
  86.     }
  87.     lsrp->next = NIL;
  88.     lsrp->prev = NIL;
  89.     lsrp->recbuf = calloc((size_t)1, lsp->lshdr.recsize);
  90.     if (lsrp->recbuf == NULL) {
  91.         LSEPRINT;
  92.         free(lsrp);
  93.         errno = ENOMEM;
  94.         return NULL;
  95.     }
  96.  
  97.     return lsrp;
  98. }
  99.  
  100. /*man---------------------------------------------------------------------------
  101. NAME
  102.      ls_rccopy - copy lseq record
  103.  
  104. SYNOPSIS
  105.      #include "lseq_.h"
  106.  
  107.      int ls_rccopy(lsp, tlsrp, slsrp)
  108.      lseq_t *lsp;
  109.      lsrec_t *tlsrp;
  110.      const lsrec_t *slsrp;
  111.  
  112. DESCRIPTION
  113.      The ls_rccopy function makes an exact copy of source record slsrp
  114.      in target record tlsrp.
  115.  
  116.      ls_rccopy will fail if one or more of the following is true:
  117.  
  118.      [EINVAL]       lsp is not a valid lseq pointer.
  119.      [EINVAL]       tlsrp or slsrp is the NULL pointer.
  120.  
  121. DIAGNOSTICS
  122.      Upon successful completion, a value of 0 is returned.  Otherwise,
  123.      a value of -1 is returned, and errno set to indicate the error.
  124.  
  125. ------------------------------------------------------------------------------*/
  126. #ifdef AC_PROTO
  127. int ls_rccopy(lseq_t *lsp, lsrec_t *tlsrp, const lsrec_t *slsrp)
  128. #else
  129. int ls_rccopy(lsp, tlsrp, slsrp)
  130. lseq_t *lsp;
  131. lsrec_t *tlsrp;
  132. const lsrec_t *slsrp;
  133. #endif
  134. {
  135. #ifdef DEBUG
  136.     /* validate arguments */
  137.     if (!ls_valid(lsp) || tlsrp == NULL || slsrp == NULL) {
  138.         LSEPRINT;
  139.         errno = EINVAL;
  140.         return -1;
  141.     }
  142. #endif
  143.     /* copy record slsrp into tlsrp */
  144.     tlsrp->next = slsrp->next;
  145.     tlsrp->prev = slsrp->prev;
  146.     memcpy(tlsrp->recbuf, slsrp->recbuf, lsp->lshdr.recsize);
  147.  
  148.     return 0;
  149. }
  150.  
  151. /*man---------------------------------------------------------------------------
  152. NAME
  153.      ls_rcfree - free memory allocated for lseq record
  154.  
  155. SYNOPSIS
  156.      #include "lseq_.h"
  157.  
  158.      void ls_rcfree(lsrp)
  159.      lsrec_t *lsrp;
  160.  
  161. DESCRIPTION
  162.      The ls_rcfree function frees all memory allocated for lseq record
  163.      lsrp.
  164.  
  165. SEE ALSO
  166.      ls_rcalloc.
  167.  
  168. ------------------------------------------------------------------------------*/
  169. #ifdef AC_PROTO
  170. void ls_rcfree(lsrec_t *lsrp)
  171. #else
  172. void ls_rcfree(lsrp)
  173. lsrec_t *lsrp;
  174. #endif
  175. {
  176.     if (lsrp != NULL) {
  177.         if (lsrp->recbuf != NULL) {
  178.             free(lsrp->recbuf);
  179.             lsrp->recbuf = NULL;
  180.         }
  181.         free(lsrp);
  182.     }
  183.  
  184.     return;
  185. }
  186.  
  187. /*man---------------------------------------------------------------------------
  188. NAME
  189.      ls_rcget - lseq record get
  190.  
  191. SYNOPSIS
  192.      #include "lseq_.h"
  193.  
  194.      int ls_rcget(lsp, lspos, lsrp)
  195.      lseq_t *lsp;
  196.      lspos_t lspos;
  197.      lsrec_t *lsrp;
  198.  
  199. DESCRIPTION
  200.      The ls_rcget function reads the record at position lspos into the
  201.      record pointed to be lsrp.  The entire record is read, including
  202.      the links.
  203.  
  204. SEE ALSO
  205.      ls_rcput.
  206.  
  207. DIAGNOSTICS
  208.      Upon successful completion, a value of 0 is returned.  Otherwise,
  209.      a value of -1 is returned, and errno set to indicate the error.
  210.  
  211. ------------------------------------------------------------------------------*/
  212. #ifdef AC_PROTO
  213. int ls_rcget(lseq_t *lsp, lspos_t lspos, lsrec_t *lsrp)
  214. #else
  215. int ls_rcget(lsp, lspos, lsrp)
  216. lseq_t *lsp;
  217. lspos_t lspos;
  218. lsrec_t *lsrp;
  219. #endif
  220. {
  221.     void *buf = NULL;
  222. #ifdef DEBUG
  223.     /* validate arguments */
  224.     if (!ls_valid(lsp) || lsrp == NULL || lspos == NIL) {
  225.         LSEPRINT;
  226.         errno = EINVAL;
  227.         return -1;
  228.     }
  229.  
  230.     /* check if not open */
  231.     if (!(lsp->flags & LSOPEN)) {
  232.         LSEPRINT;
  233.         errno = LSENOPEN;
  234.         return -1;
  235.     }
  236. #endif
  237.     /* read record from file */
  238.     buf = calloc((size_t)1, ls_blksize(lsp));
  239.     if (buf == NULL) {
  240.         LSEPRINT;
  241.         errno = ENOMEM;
  242.         return -1;
  243.     }
  244.     if (bgetb(lsp->bp, (bpos_t)lspos, buf) == -1) {
  245.         LSEPRINT;
  246.         free(buf);
  247.         return -1;
  248.     }
  249.  
  250.     /* convert record from file format */
  251.     memcpy(lsrp, buf, offsetof(lsrec_t, recbuf));
  252.     memcpy(lsrp->recbuf, ((char *)buf + offsetof(lsrec_t, recbuf)), lsp->lshdr.recsize);
  253.  
  254.     /* free buffer */
  255.     free(buf);
  256.     buf = NULL;
  257.  
  258.     return 0;
  259. }
  260.  
  261. /*man---------------------------------------------------------------------------
  262. NAME
  263.      ls_rcinit - lseq record initialize
  264.  
  265. SYNOPSIS
  266.      #include "lseq_.h"
  267.  
  268.      void ls_rcinit(lsp, lsrp)
  269.      lseq_t *lsp;
  270.      lsrec_t *lsrp;
  271.  
  272. DESCRIPTION
  273.      The ls_rcinit function initializes record lsrp.
  274.  
  275. ------------------------------------------------------------------------------*/
  276. #ifdef AC_PROTO
  277. void ls_rcinit(lseq_t *lsp, lsrec_t *lsrp)
  278. #else
  279. void ls_rcinit(lsp, lsrp)
  280. lseq_t *lsp;
  281. lsrec_t *lsrp;
  282. #endif
  283. {
  284. #ifdef DEBUG
  285.     /* validate arguments */
  286.     if (!ls_valid(lsp) || lsrp == NULL) {
  287.         LSEPRINT;
  288.         return;
  289.     }
  290. #endif
  291.     /* initialize lsrp */
  292.     lsrp->next = NIL;
  293.     lsrp->prev = NIL;
  294.     if (lsrp->recbuf == NULL) {
  295.         LSEPRINT;
  296.         return;
  297.     }
  298.  
  299.     memset(lsrp->recbuf, 0, lsp->lshdr.recsize);
  300.  
  301.     return;
  302. }
  303.  
  304. /*man---------------------------------------------------------------------------
  305. NAME
  306.      ls_rcput - lseq record put
  307.  
  308. SYNOPSIS
  309.      #include "lseq_.h"
  310.  
  311.      int ls_rcput(lsp, lspos, lsrp)
  312.      lseq_t *lsp;
  313.      lspos_t lspos;
  314.      const lsrec_t *lsrp;
  315.  
  316. DESCRIPTION
  317.      The ls_rcput function writes the record pointed to by lsrp into
  318.      record position lspos.  The entire record is written, including
  319.      the links.
  320.  
  321.      ls_rcput will fail if one or more of the following is true:
  322.  
  323.      [EINVAL]       lsp is not a valid lseq pointer.
  324.      [EINVAL]       lspos is NIL.
  325.      [LSENOPEN]     lsp is not open.
  326.  
  327. SEE ALSO
  328.      ls_rcget, ls_rcputf.
  329.  
  330. DIAGNOSTICS
  331.      Upon successful completion, a value of 0 is returned.  Otherwise,
  332.      a value of -1 is returned, and errno set to indicate the error.
  333.  
  334. ------------------------------------------------------------------------------*/
  335. #ifdef AC_PROTO
  336. int ls_rcput(lseq_t *lsp, lspos_t lspos, const lsrec_t *lsrp)
  337. #else
  338. int ls_rcput(lsp, lspos, lsrp)
  339. lseq_t *lsp;
  340. lspos_t lspos;
  341. const lsrec_t *lsrp;
  342. #endif
  343. {
  344.     void *buf = NULL;
  345. #ifdef DEBUG
  346.     /* validate arguments */
  347.     if (!ls_valid(lsp) || lspos == NIL || lsrp == NULL) {
  348.         LSEPRINT;
  349.         errno = EINVAL;
  350.         return -1;
  351.     }
  352.  
  353.     /* check if not open */
  354.     if (!(lsp->flags & LSOPEN)) {
  355.         LSEPRINT;
  356.         errno = LSENOPEN;
  357.         return -1;
  358.     }
  359. #endif
  360.     /* convert record to file format */
  361.     buf = calloc((size_t)1, ls_blksize(lsp));
  362.     if (buf == NULL) {
  363.         LSEPRINT;
  364.         errno = ENOMEM;
  365.         return -1;
  366.     }
  367.     memcpy(buf, lsrp, offsetof(lsrec_t, recbuf));
  368.     memcpy(((char *)buf + offsetof(lsrec_t, recbuf)), lsrp->recbuf, lsp->lshdr.recsize);
  369.  
  370.     /* write record to file */
  371.     if (bputb(lsp->bp, (bpos_t)lspos, buf) == -1) {
  372.         LSEPRINT;
  373.         free(buf);
  374.         return -1;
  375.     }
  376.  
  377.     /* free buffer */
  378.     free(buf);
  379.     buf = NULL;
  380.  
  381.     return 0;
  382. }
  383.  
  384. /*man---------------------------------------------------------------------------
  385. NAME
  386.      ls_rcputf - lseq record field put
  387.  
  388. SYNOPSIS
  389.      #include "lseq_.h"
  390.  
  391.      int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  392.      lseq_t *lsp;
  393.      lspos_t lspos;
  394.      size_t offset;
  395.      const void *buf;
  396.      size_t bufsize;
  397.  
  398. DESCRIPTION
  399.      The ls_rcputf function writes the field pointed to by buf into
  400.      record position lspos.  Only the field is written.
  401.  
  402.      ls_rcputf will fail if one or more of the following is true:
  403.  
  404.      [EINVAL]       lsp is not a valid lseq pointer.
  405.      [EINVAL]       lspos is NIL.
  406.      [LSEBOUND]
  407.      [LSENOPEN]
  408.  
  409. SEE ALSO
  410.      ls_rcget, ls_rcput.
  411.  
  412. DIAGNOSTICS
  413.      Upon successful completion, a value of 0 is returned.  Otherwise,
  414.      a value of -1 is returned, and errno set to indicate the error.
  415.  
  416. ------------------------------------------------------------------------------*/
  417. #ifdef AC_PROTO
  418. int ls_rcputf(lseq_t *lsp, lspos_t lspos, size_t offset, const void *buf, size_t bufsize)
  419. #else
  420. int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  421. lseq_t *lsp;
  422. lspos_t lspos;
  423. size_t offset;
  424. const void *buf;
  425. size_t bufsize;
  426. #endif
  427. {
  428. #ifdef DEBUG
  429.     /* validate arguments */
  430.     if (!ls_valid(lsp) || lspos == NIL || buf == NULL || bufsize < 1) {
  431.         LSEPRINT;
  432.         errno = EINVAL;
  433.         return -1;
  434.     }
  435.  
  436.     /* check if not open */
  437.     if (!(lsp->flags & LSOPEN)) {
  438.         LSEPRINT;
  439.         errno = LSENOPEN;
  440.         return -1;
  441.     }
  442.  
  443.     /* check if record boundary crossed */
  444.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  445.         LSEPRINT;
  446.         errno = LSEBOUND;
  447.         return -1;
  448.     }
  449. #endif
  450.     /* write record to file */
  451.     if (bputbf(lsp->bp, (bpos_t)lspos, offsetof(lsrec_t, recbuf) + offset, buf, bufsize) == -1) {
  452.         LSEPRINT;
  453.         return -1;
  454.     }
  455.  
  456.     return 0;
  457. }
  458.